Entrega grupal

Álvaro Pascual (DNI: 01672747-A), Rodrigo Castillo (DNI: 29624141-A), Marcos Martínez (DNI: 50635315-X), Jorge Mendoza (DNI: 51518202-L)

Paquetes necesarios

rm(list = ls())
library(tidyverse)
library(glue)
library(lubridate)
library(forcats)
library(dplyr)
library(ggplot2)
library(plotly)
library(patchwork)

Datos

Un viejo amigo: la práctica se basará en los archivos de datos electorales que se indican a continuación, recopilando datos sobre las elecciones al Congreso de los Diputados en España desde 2008 hasta la actualidad, así como encuestas, códigos de municipios y abreviaturas

election_data <- read_csv(file = "./data/datos_elecciones_brutos.csv")
cod_mun <- read_csv(file = "./data/cod_mun.csv")
surveys <- read_csv(file = "./data/historical_surveys.csv")
abbrev <- read_csv(file = "./data/siglas.csv")

Pasamos la tabla de elecciones a tidy, hacemos una columna partido

election_tidy <- election_data |> 
  pivot_longer(cols = -c(tipo_eleccion:votos_candidaturas), names_to = "Partido", values_to = "votos") |> drop_na("votos")

Agrupar varios partidos a la misma sigla

abbrev_g<-abbrev |> 
  mutate(siglas=case_when(
    str_detect(denominacion, "PARTIDO SOCIALISTA|PSOE") ~ "PSOE",
    str_detect(denominacion, "PARTIDO POPULAR") ~ "PP",
    str_detect(denominacion, "CIUDADANOS-PARTIDO DE LA CIUDADANÍA|CIUDADANOS-PARTIDO DE LA CIUDADANIA") ~ "C's",
    str_detect(denominacion, "PARTIDO NACIONALISTA VASCO") ~ "PNV",
    str_detect(denominacion, "BLOQUE NACIONALISTA GALEGO") ~ "BNG",
    str_detect(denominacion, "CONVERGENCIA i UNIO|CONVERGENCIA I UNIO") ~ "CIU",
    str_detect(denominacion, "UNIDAS PODEMOS|PODEM|EZKER BATUA|IZQUIERDA UNIDA") ~ "UP",
    str_detect(denominacion, "ESQUERRA REPUBLICANA DE CATALUNYA") ~ "ERC",
    str_detect(denominacion, "SORTU|EUSKO|ALkARTASUNA|ARALAR|ALTERNATIBA") ~ "EH-BILDU",
    str_detect(denominacion, "MÁS PAÍS") ~ "MP",
    str_detect(denominacion, "VOX") ~ "VOX",
    TRUE ~ "OTROS",
  ))

Hacemos que si un partido tienen asiganda varias abreviaturas se quede con la primera y UNimos elecciones con abreaviaturas, para que en elecciones salga la abrevaitura de cada partido

siglas_unique <- abbrev_g |> 
  distinct(denominacion, .keep_all = TRUE)

election_tidy_with_siglas <- election_tidy |> 
  left_join(siglas_unique, 
            by = c("Partido" = "denominacion"))

Separamos cod_mun en las 3 varibales de comunidad autonoma, provincia y municipio, para que coincidan con las de la tabla elecciones

cod_mun <- cod_mun |> 
  separate(cod_mun, 
           into = c("codigo_ccaa", "codigo_provincia", "codigo_municipio"), 
           sep = "-")

En la tabla surveys (encuestas) hacemos una columna partido, muy simliar al primero de elecciones, además quitamos los partidos que no tienen importancia en la encuesta, es decir que no han recibido datos

surveys_tidy <- surveys |> 
  pivot_longer(cols = -c(type_survey:turnout), names_to = "Partido", values_to = "value") |> 
  filter(year(date_elec) >= 2008 & year(date_elec) <= 2019) |> 
  drop_na("value")

Debes descartar las encuestas que: se refieran a elecciones anteriores a 2008, sean a pie de urna, tamaño muestral desconocido o inferior a 500, tenga 1 día o menos de trabajo de campo.

# Convertir las columnas de fecha a formato Date
surveys_clean <- surveys_tidy |> 
  mutate(
    date_elec = ymd(date_elec),
    field_date_from = ymd(field_date_from),
    field_date_to = ymd(field_date_to),
    field_duration = as.numeric(field_date_to - field_date_from)
  )

# Filtrar la base de datos según las condiciones
surveys_clean <- surveys_clean |> 
  filter(
    date_elec >= "2008-01-01",         
    exit_poll == FALSE,                
    !is.na(size) & size >= 500,        
    field_duration > 1                 
  )

12

a) ¿Qué partido fue el ganador en los municipios con más de 100.000 habitantes (censo) en cada una de las elecciones?

resultados_grandes_municipios <- election_tidy_with_siglas |> 
  filter(censo > 100000) |> 
  group_by(anno, Partido) |> 
  summarise(
    total_votos = sum(votos, na.rm = TRUE),
    .groups = "drop"  
  ) |> 
  group_by(anno) |> 
  slice_max(order_by = total_votos, n = 1) |> 
  ungroup()  
# b) ¿Qué partido fue el segundo cuando el primero fue el PSOE? ¿Y cuando el primero fue el PP? `
# c) ¿A quién beneficia la baja participación?
::: {.cell}
```{.r .cell-code} election_tidy_with_participation <- election_tidy_with_siglas |> mutate(participacion = votos / censo)
participacion_partido <- election_tidy_with_participation |> group_by(anno, siglas) |> summarise( participacion_media = mean(participacion, na.rm = TRUE), votos_totales = sum(votos, na.rm = TRUE), .groups = “drop” )
resultados_con_participacion <- participacion_partido |> mutate(resultado_votos = votos_totales / sum(votos_totales) * 100)
colores_partidos <- c( “PSOE” = “#E30000”, “PP” = “#0000FF”, “C’s” = “#FF6600”, “PNV” = “#006747”, “BNG” = “#0C6F6D”, “CIU” = “#9C3D28”, “UP” = “#6A0DAD”, “ERC” = “#E24D3A”, “EH-BILDU” = “#006F6F”, “MP” = “#00CFFF”, “VOX” = “#008000”, “OTROS” = “#BEBEBE” )
# Crear la gráfica de dispersión con los colores definidos y mejor estética resultados <- ggplot(resultados_con_participacion, aes(x = participacion_media, y = resultado_votos, color = siglas)) + geom_point(size = 3, alpha = 0.7) + # Puntos con tamaño ajustado y transparencia geom_smooth(method = “lm”, se = FALSE, aes(group = siglas), color = “black”, linetype = “dashed”) + # Línea de regresión por partido facet_wrap(~ anno, scales = “free_y”) + # Facetas por año scale_color_manual(values = colores_partidos) + # Usar los colores definidos labs( title = “Relación entre Participación y Resultados Electorales por Año”, subtitle = “Cada punto representa un partido en una elección, con la participación y el porcentaje de votos”, x = “Participación Media (%)”, y = “Porcentaje de Votos (%)”, color = “Partido” ) + theme_minimal(base_size = 14) + # Estilo minimalista y tamaño de texto ajustado theme( legend.position = “bottom”, # Colocar la leyenda abajo legend.title = element_text(face = “bold”, size = 12), # Estilo de título de la leyenda legend.text = element_text(size = 10), # Estilo de texto de la leyenda strip.text = element_text(face = “bold”, size = 12), # Estilo de los títulos de las facetas plot.title = element_text(face = “bold”, size = 16, hjust = 0.5), # Estilo del título plot.subtitle = element_text(size = 12, hjust = 0.5) # Estilo del subtítulo )
resultados_interactivo <- ggplotly(resultados) resultados_interactivo ```
::: {.cell-output-display}
{=html} <div class="plotly html-widget html-fill-item" id="htmlwidget-391f02a6581e7340334f" style="width:960px;height:480px;"></div> <script type="application/json" data-for="htmlwidget-391f02a6581e7340334f">{"x":{"data":[{"x":[0.092024570442465786],"y":[0.14210758645008228],"text":"participacion_media: 0.092024570<br />resultado_votos: 0.14210759<br />siglas: BNG","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(12,111,109,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(12,111,109,1)"}},"hoveron":"points","name":"BNG","legendgroup":"BNG","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[0.075231160197395813],"y":[0.12433752246075291],"text":"participacion_media: 0.075231160<br />resultado_votos: 0.12433752<br />siglas: BNG","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(12,111,109,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(12,111,109,1)"}},"hoveron":"points","name":"BNG","legendgroup":"BNG","showlegend":false,"xaxis":"x2","yaxis":"y2","hoverinfo":"text","frame":null},{"x":[0.043270181843673192],"y":[0.14480542807907054],"text":"participacion_media: 0.043270182<br />resultado_votos: 0.14480543<br />siglas: BNG","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(12,111,109,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(12,111,109,1)"}},"hoveron":"points","name":"BNG","legendgroup":"BNG","showlegend":false,"xaxis":"x2","yaxis":"y5","hoverinfo":"text","frame":null},{"x":[0.0019474763049230298],"y":[0.014197595132029762],"text":"participacion_media: 0.001947476<br />resultado_votos: 0.01419760<br />siglas: C's","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(255,102,0,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(255,102,0,1)"}},"hoveron":"points","name":"C's","legendgroup":"C's","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[0.079403425714033568],"y":[1.7740575006131212],"text":"participacion_media: 0.079403426<br />resultado_votos: 1.77405750<br />siglas: C's","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(255,102,0,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(255,102,0,1)"}},"hoveron":"points","name":"C's","legendgroup":"C's","showlegend":false,"xaxis":"x3","yaxis":"y3","hoverinfo":"text","frame":null},{"x":[0.073558761920329951],"y":[1.8625733044825314],"text":"participacion_media: 0.073558762<br />resultado_votos: 1.86257330<br />siglas: C's","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(255,102,0,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(255,102,0,1)"}},"hoveron":"points","name":"C's","legendgroup":"C's","showlegend":false,"xaxis":"x","yaxis":"y4","hoverinfo":"text","frame":null},{"x":[0.075964862032643965],"y":[3.6998909843995271],"text":"participacion_media: 0.075964862<br />resultado_votos: 3.69989098<br />siglas: C's","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(255,102,0,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(255,102,0,1)"}},"hoveron":"points","name":"C's","legendgroup":"C's","showlegend":false,"xaxis":"x2","yaxis":"y5","hoverinfo":"text","frame":null},{"x":[0.2318159253374604],"y":[0.52547183187354951],"text":"participacion_media: 0.231815925<br />resultado_votos: 0.52547183<br />siglas: CIU","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(156,61,40,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(156,61,40,1)"}},"hoveron":"points","name":"CIU","legendgroup":"CIU","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[0.033207868967404783],"y":[0.042505933351695301],"text":"participacion_media: 0.033207869<br />resultado_votos: 0.04250593<br />siglas: EH-BILDU","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(0,111,111,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(0,111,111,1)"}},"hoveron":"points","name":"EH-BILDU","legendgroup":"EH-BILDU","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[0.098856896350437123],"y":[0.19653057023686832],"text":"participacion_media: 0.098856896<br />resultado_votos: 0.19653057<br />siglas: ERC","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(226,77,58,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(226,77,58,1)"}},"hoveron":"points","name":"ERC","legendgroup":"ERC","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[0.073819527542602723],"y":[0.16576526910508849],"text":"participacion_media: 0.073819528<br />resultado_votos: 0.16576527<br />siglas: ERC","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(226,77,58,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(226,77,58,1)"}},"hoveron":"points","name":"ERC","legendgroup":"ERC","showlegend":false,"xaxis":"x2","yaxis":"y2","hoverinfo":"text","frame":null},{"x":[0.17927096651945423],"y":[0.40669487584880232],"text":"participacion_media: 0.179270967<br />resultado_votos: 0.40669488<br />siglas: ERC","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(226,77,58,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(226,77,58,1)"}},"hoveron":"points","name":"ERC","legendgroup":"ERC","showlegend":false,"xaxis":"x3","yaxis":"y3","hoverinfo":"text","frame":null},{"x":[0.25554179097580176],"y":[1.2792885449644433],"text":"participacion_media: 0.255541791<br />resultado_votos: 1.27928854<br />siglas: ERC","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(226,77,58,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(226,77,58,1)"}},"hoveron":"points","name":"ERC","legendgroup":"ERC","showlegend":false,"xaxis":"x2","yaxis":"y5","hoverinfo":"text","frame":null},{"x":[0.012237249723629176],"y":[0.25120732484086805],"text":"participacion_media: 0.012237250<br />resultado_votos: 0.25120732<br />siglas: MP","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(0,207,255,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(0,207,255,1)"}},"hoveron":"points","name":"MP","legendgroup":"MP","showlegend":true,"xaxis":"x2","yaxis":"y5","hoverinfo":"text","frame":null},{"x":[0.01149437150064758],"y":[1.923343951357644],"text":"participacion_media: 0.011494372<br />resultado_votos: 1.92334395<br />siglas: OTROS","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(190,190,190,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(190,190,190,1)"}},"hoveron":"points","name":"OTROS","legendgroup":"OTROS","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[0.019771527818947893],"y":[2.4930601145339621],"text":"participacion_media: 0.019771528<br />resultado_votos: 2.49306011<br />siglas: OTROS","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(190,190,190,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(190,190,190,1)"}},"hoveron":"points","name":"OTROS","legendgroup":"OTROS","showlegend":false,"xaxis":"x2","yaxis":"y2","hoverinfo":"text","frame":null},{"x":[0.021831570099067586],"y":[1.5238849364574159],"text":"participacion_media: 0.021831570<br />resultado_votos: 1.52388494<br />siglas: OTROS","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(190,190,190,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(190,190,190,1)"}},"hoveron":"points","name":"OTROS","legendgroup":"OTROS","showlegend":false,"xaxis":"x3","yaxis":"y3","hoverinfo":"text","frame":null},{"x":[0.022841878128504019],"y":[1.2299565837486581],"text":"participacion_media: 0.022841878<br />resultado_votos: 1.22995658<br />siglas: OTROS","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(190,190,190,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(190,190,190,1)"}},"hoveron":"points","name":"OTROS","legendgroup":"OTROS","showlegend":false,"xaxis":"x","yaxis":"y4","hoverinfo":"text","frame":null},{"x":[0.034522340643855509],"y":[2.7788135185641361],"text":"participacion_media: 0.034522341<br />resultado_votos: 2.77881352<br />siglas: OTROS","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(190,190,190,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(190,190,190,1)"}},"hoveron":"points","name":"OTROS","legendgroup":"OTROS","showlegend":false,"xaxis":"x2","yaxis":"y5","hoverinfo":"text","frame":null},{"x":[0.24701923625171995],"y":[0.20577420618045661],"text":"participacion_media: 0.247019236<br />resultado_votos: 0.20577421<br />siglas: PNV","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(0,103,71,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(0,103,71,1)"}},"hoveron":"points","name":"PNV","legendgroup":"PNV","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[0.23905096171795837],"y":[0.21956671794918003],"text":"participacion_media: 0.239050962<br />resultado_votos: 0.21956672<br />siglas: PNV","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(0,103,71,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(0,103,71,1)"}},"hoveron":"points","name":"PNV","legendgroup":"PNV","showlegend":false,"xaxis":"x2","yaxis":"y2","hoverinfo":"text","frame":null},{"x":[0.22786867075894693],"y":[0.20463834428736649],"text":"participacion_media: 0.227868671<br />resultado_votos: 0.20463834<br />siglas: PNV","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(0,103,71,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(0,103,71,1)"}},"hoveron":"points","name":"PNV","legendgroup":"PNV","showlegend":false,"xaxis":"x3","yaxis":"y3","hoverinfo":"text","frame":null},{"x":[0.21616635556887023],"y":[0.1941869221014243],"text":"participacion_media: 0.216166356<br />resultado_votos: 0.19418692<br />siglas: PNV","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(0,103,71,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(0,103,71,1)"}},"hoveron":"points","name":"PNV","legendgroup":"PNV","showlegend":false,"xaxis":"x","yaxis":"y4","hoverinfo":"text","frame":null},{"x":[0.27162498221220127],"y":[0.52395056403346096],"text":"participacion_media: 0.271624982<br />resultado_votos: 0.52395056<br />siglas: PNV","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(0,103,71,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(0,103,71,1)"}},"hoveron":"points","name":"PNV","legendgroup":"PNV","showlegend":false,"xaxis":"x2","yaxis":"y5","hoverinfo":"text","frame":null},{"x":[0.36277736677763112],"y":[5.9298742208693085],"text":"participacion_media: 0.362777367<br />resultado_votos: 5.92987422<br />siglas: PP","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(0,0,255,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(0,0,255,1)"}},"hoveron":"points","name":"PP","legendgroup":"PP","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[0.39409026454998242],"y":[6.8644680361135153],"text":"participacion_media: 0.394090265<br />resultado_votos: 6.86446804<br />siglas: PP","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(0,0,255,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(0,0,255,1)"}},"hoveron":"points","name":"PP","legendgroup":"PP","showlegend":false,"xaxis":"x2","yaxis":"y2","hoverinfo":"text","frame":null},{"x":[0.2894533404157964],"y":[4.430865609814802],"text":"participacion_media: 0.289453340<br />resultado_votos: 4.43086561<br />siglas: PP","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(0,0,255,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(0,0,255,1)"}},"hoveron":"points","name":"PP","legendgroup":"PP","showlegend":false,"xaxis":"x3","yaxis":"y3","hoverinfo":"text","frame":null},{"x":[0.30932875262026288],"y":[4.8803235824904974],"text":"participacion_media: 0.309328753<br />resultado_votos: 4.88032358<br />siglas: PP","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(0,0,255,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(0,0,255,1)"}},"hoveron":"points","name":"PP","legendgroup":"PP","showlegend":false,"xaxis":"x","yaxis":"y4","hoverinfo":"text","frame":null},{"x":[0.21602920014347168],"y":[5.9925841110481555],"text":"participacion_media: 0.216029200<br />resultado_votos: 5.99258411<br />siglas: PP","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(0,0,255,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(0,0,255,1)"}},"hoveron":"points","name":"PP","legendgroup":"PP","showlegend":false,"xaxis":"x2","yaxis":"y5","hoverinfo":"text","frame":null},{"x":[0.33200137175820282],"y":[5.733195730744332],"text":"participacion_media: 0.332001372<br />resultado_votos: 5.73319573<br />siglas: PSOE","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(227,0,0,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(227,0,0,1)"}},"hoveron":"points","name":"PSOE","legendgroup":"PSOE","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[0.23379055934558127],"y":[3.6295828973993998],"text":"participacion_media: 0.233790559<br />resultado_votos: 3.62958290<br />siglas: PSOE","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(227,0,0,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(227,0,0,1)"}},"hoveron":"points","name":"PSOE","legendgroup":"PSOE","showlegend":false,"xaxis":"x2","yaxis":"y2","hoverinfo":"text","frame":null},{"x":[0.19297966244081474],"y":[3.3928717215931661],"text":"participacion_media: 0.192979662<br />resultado_votos: 3.39287172<br />siglas: PSOE","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(227,0,0,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(227,0,0,1)"}},"hoveron":"points","name":"PSOE","legendgroup":"PSOE","showlegend":false,"xaxis":"x3","yaxis":"y3","hoverinfo":"text","frame":null},{"x":[0.19582889456085273],"y":[3.1907880487678573],"text":"participacion_media: 0.195828895<br />resultado_votos: 3.19078805<br />siglas: PSOE","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(227,0,0,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(227,0,0,1)"}},"hoveron":"points","name":"PSOE","legendgroup":"PSOE","showlegend":false,"xaxis":"x","yaxis":"y4","hoverinfo":"text","frame":null},{"x":[0.2235644685457546],"y":[8.5198483248466221],"text":"participacion_media: 0.223564469<br />resultado_votos: 8.51984832<br />siglas: PSOE","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(227,0,0,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(227,0,0,1)"}},"hoveron":"points","name":"PSOE","legendgroup":"PSOE","showlegend":false,"xaxis":"x2","yaxis":"y5","hoverinfo":"text","frame":null},{"x":[0.021360245952019932],"y":[0.28234106862972552],"text":"participacion_media: 0.021360246<br />resultado_votos: 0.28234107<br />siglas: UP","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(106,13,173,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(106,13,173,1)"}},"hoveron":"points","name":"UP","legendgroup":"UP","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[0.038127124336627891],"y":[0.23880648137723895],"text":"participacion_media: 0.038127124<br />resultado_votos: 0.23880648<br />siglas: UP","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(106,13,173,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(106,13,173,1)"}},"hoveron":"points","name":"UP","legendgroup":"UP","showlegend":false,"xaxis":"x2","yaxis":"y2","hoverinfo":"text","frame":null},{"x":[0.07498351523139242],"y":[3.6237210629344028],"text":"participacion_media: 0.074983515<br />resultado_votos: 3.62372106<br />siglas: UP","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(106,13,173,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(106,13,173,1)"}},"hoveron":"points","name":"UP","legendgroup":"UP","showlegend":false,"xaxis":"x3","yaxis":"y3","hoverinfo":"text","frame":null},{"x":[0.10463730076701624],"y":[2.5600440752412288],"text":"participacion_media: 0.104637301<br />resultado_votos: 2.56004408<br />siglas: UP","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(106,13,173,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(106,13,173,1)"}},"hoveron":"points","name":"UP","legendgroup":"UP","showlegend":false,"xaxis":"x","yaxis":"y4","hoverinfo":"text","frame":null},{"x":[0.06991338934175792],"y":[4.3377927877410949],"text":"participacion_media: 0.069913389<br />resultado_votos: 4.33779279<br />siglas: UP","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(106,13,173,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(106,13,173,1)"}},"hoveron":"points","name":"UP","legendgroup":"UP","showlegend":false,"xaxis":"x2","yaxis":"y5","hoverinfo":"text","frame":null},{"x":[0.0035142013504288966],"y":[0.039145166352606058],"text":"participacion_media: 0.003514201<br />resultado_votos: 0.03914517<br />siglas: VOX","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(0,128,0,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(0,128,0,1)"}},"hoveron":"points","name":"VOX","legendgroup":"VOX","showlegend":true,"xaxis":"x3","yaxis":"y3","hoverinfo":"text","frame":null},{"x":[0.0032079317025824903],"y":[0.031696925014224733],"text":"participacion_media: 0.003207932<br />resultado_votos: 0.03169693<br />siglas: VOX","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(0,128,0,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(0,128,0,1)"}},"hoveron":"points","name":"VOX","legendgroup":"VOX","showlegend":false,"xaxis":"x","yaxis":"y4","hoverinfo":"text","frame":null},{"x":[0.091757852540800086],"y":[4.286419124950454],"text":"participacion_media: 0.091757853<br />resultado_votos: 4.28641912<br />siglas: VOX","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(0,128,0,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(0,128,0,1)"}},"hoveron":"points","name":"VOX","legendgroup":"VOX","showlegend":false,"xaxis":"x2","yaxis":"y5","hoverinfo":"text","frame":null},{"x":[0.049908683830323021],"y":[2.0147916902569487],"text":"participacion_media: 0.049908684<br />resultado_votos: 2.01479169<br />siglas: NA","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(127,127,127,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(127,127,127,1)"}},"hoveron":"points","name":"NA","legendgroup":"NA","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[0.046812301341049224],"y":[2.4935669776367928],"text":"participacion_media: 0.046812301<br />resultado_votos: 2.49356698<br />siglas: NA","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(127,127,127,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(127,127,127,1)"}},"hoveron":"points","name":"NA","legendgroup":"NA","showlegend":false,"xaxis":"x2","yaxis":"y2","hoverinfo":"text","frame":null},{"x":[0.047819580397047146],"y":[1.4967267092943739],"text":"participacion_media: 0.047819580<br />resultado_votos: 1.49672671<br />siglas: NA","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(127,127,127,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(127,127,127,1)"}},"hoveron":"points","name":"NA","legendgroup":"NA","showlegend":false,"xaxis":"x3","yaxis":"y3","hoverinfo":"text","frame":null},{"x":[0.066466534339712863],"y":[2.168542200377003],"text":"participacion_media: 0.066466534<br />resultado_votos: 2.16854220<br />siglas: NA","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(127,127,127,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(127,127,127,1)"}},"hoveron":"points","name":"NA","legendgroup":"NA","showlegend":false,"xaxis":"x","yaxis":"y4","hoverinfo":"text","frame":null},{"x":[0.016880623477801233],"y":[1.9353933154541161],"text":"participacion_media: 0.016880623<br />resultado_votos: 1.93539332<br />siglas: NA","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(127,127,127,1)","opacity":0.69999999999999996,"size":11.338582677165356,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(127,127,127,1)"}},"hoveron":"points","name":"NA","legendgroup":"NA","showlegend":false,"xaxis":"x2","yaxis":"y5","hoverinfo":"text","frame":null},{"visible":false,"showlegend":false,"xaxis":null,"yaxis":null,"hoverinfo":"text","frame":null}],"layout":{"margin":{"t":71.790784557907841,"r":9.2984640929846396,"b":56.720630967206304,"l":39.983395599833955},"font":{"color":"rgba(0,0,0,1)","family":"","size":18.596928185969279},"title":{"text":"<b> Relación entre Participación y Resultados Electorales por Año <\/b>","font":{"color":"rgba(0,0,0,1)","family":"","size":21.253632212536321},"x":0.5,"xref":"paper"},"xaxis":{"domain":[0,0.32679535076795346],"automargin":true,"type":"linear","autorange":false,"range":[-0.017659663107329939,0.41369740396223537],"tickmode":"array","ticktext":["0.0","0.1","0.2","0.3","0.4"],"tickvals":[0,0.10000000000000002,0.19999999999999998,0.30000000000000004,0.40000000000000002],"categoryorder":"array","categoryarray":["0.0","0.1","0.2","0.3","0.4"],"nticks":null,"ticks":"","tickcolor":null,"ticklen":4.6492320464923198,"tickwidth":0,"showticklabels":true,"tickfont":{"color":"rgba(77,77,77,1)","family":"","size":14.877542548775427},"tickangle":-0,"showline":false,"linecolor":null,"linewidth":0,"showgrid":true,"gridcolor":"rgba(235,235,235,1)","gridwidth":0.8453149175440583,"zeroline":false,"anchor":"y4","title":"","hoverformat":".2f"},"annotations":[{"text":"Participación Media (%)","x":0.5,"y":0,"showarrow":false,"ax":0,"ay":0,"font":{"color":"rgba(0,0,0,1)","family":"","size":18.596928185969279},"xref":"paper","yref":"paper","textangle":-0,"xanchor":"center","yanchor":"top","annotationType":"axis","yshift":-27.895392278953928},{"text":"Porcentaje de Votos (%)","x":0,"y":0.5,"showarrow":false,"ax":0,"ay":0,"font":{"color":"rgba(0,0,0,1)","family":"","size":18.596928185969279},"xref":"paper","yref":"paper","textangle":-90,"xanchor":"right","yanchor":"center","annotationType":"axis","xshift":-20.456621004566209},{"text":"2008","x":0.16339767538397673,"y":1,"showarrow":false,"ax":0,"ay":0,"font":{"color":"rgba(26,26,26,1)","family":"","size":15.940224159402243},"xref":"paper","yref":"paper","textangle":-0,"xanchor":"center","yanchor":"bottom"},{"text":"2011","x":0.5,"y":1,"showarrow":false,"ax":0,"ay":0,"font":{"color":"rgba(26,26,26,1)","family":"","size":15.940224159402243},"xref":"paper","yref":"paper","textangle":-0,"xanchor":"center","yanchor":"bottom"},{"text":"2015","x":0.83660232461602324,"y":1,"showarrow":false,"ax":0,"ay":0,"font":{"color":"rgba(26,26,26,1)","family":"","size":15.940224159402243},"xref":"paper","yref":"paper","textangle":-0,"xanchor":"center","yanchor":"bottom"},{"text":"2016","x":0.16339767538397673,"y":0.48028227480282276,"showarrow":false,"ax":0,"ay":0,"font":{"color":"rgba(26,26,26,1)","family":"","size":15.940224159402243},"xref":"paper","yref":"paper","textangle":-0,"xanchor":"center","yanchor":"bottom"},{"text":"2019","x":0.5,"y":0.48028227480282276,"showarrow":false,"ax":0,"ay":0,"font":{"color":"rgba(26,26,26,1)","family":"","size":15.940224159402243},"xref":"paper","yref":"paper","textangle":-0,"xanchor":"center","yanchor":"bottom"}],"yaxis":{"domain":[0.51971772519717729,1],"automargin":true,"type":"linear","autorange":false,"range":[-0.28158623615483414,6.225658052156172],"tickmode":"array","ticktext":["0","2","4","6"],"tickvals":[0,2,4,6],"categoryorder":"array","categoryarray":["0","2","4","6"],"nticks":null,"ticks":"","tickcolor":null,"ticklen":4.6492320464923198,"tickwidth":0,"showticklabels":true,"tickfont":{"color":"rgba(77,77,77,1)","family":"","size":14.877542548775427},"tickangle":-0,"showline":false,"linecolor":null,"linewidth":0,"showgrid":true,"gridcolor":"rgba(235,235,235,1)","gridwidth":0.8453149175440583,"zeroline":false,"anchor":"x","title":"","hoverformat":".2f"},"shapes":[{"type":"rect","fillcolor":null,"line":{"color":null,"width":0,"linetype":[]},"yref":"paper","xref":"paper","x0":0,"x1":0.32679535076795346,"y0":0.51971772519717729,"y1":1},{"type":"rect","fillcolor":null,"line":{"color":null,"width":0,"linetype":[]},"yref":"paper","xref":"paper","x0":0,"x1":0.32679535076795346,"y0":0,"y1":30.817766708177672,"yanchor":1,"ysizemode":"pixel"},{"type":"rect","fillcolor":null,"line":{"color":null,"width":0,"linetype":[]},"yref":"paper","xref":"paper","x0":0.33987131589871317,"x1":0.66012868410128678,"y0":0.51971772519717729,"y1":1},{"type":"rect","fillcolor":null,"line":{"color":null,"width":0,"linetype":[]},"yref":"paper","xref":"paper","x0":0.33987131589871317,"x1":0.66012868410128678,"y0":0,"y1":30.817766708177672,"yanchor":1,"ysizemode":"pixel"},{"type":"rect","fillcolor":null,"line":{"color":null,"width":0,"linetype":[]},"yref":"paper","xref":"paper","x0":0.67320464923204648,"x1":1,"y0":0.51971772519717729,"y1":1},{"type":"rect","fillcolor":null,"line":{"color":null,"width":0,"linetype":[]},"yref":"paper","xref":"paper","x0":0.67320464923204648,"x1":1,"y0":0,"y1":30.817766708177672,"yanchor":1,"ysizemode":"pixel"},{"type":"rect","fillcolor":null,"line":{"color":null,"width":0,"linetype":[]},"yref":"paper","xref":"paper","x0":0,"x1":0.32679535076795346,"y0":0,"y1":0.48028227480282276},{"type":"rect","fillcolor":null,"line":{"color":null,"width":0,"linetype":[]},"yref":"paper","xref":"paper","x0":0,"x1":0.32679535076795346,"y0":0,"y1":30.817766708177672,"yanchor":0.48028227480282276,"ysizemode":"pixel"},{"type":"rect","fillcolor":null,"line":{"color":null,"width":0,"linetype":[]},"yref":"paper","xref":"paper","x0":0.33987131589871317,"x1":0.66012868410128678,"y0":0,"y1":0.48028227480282276},{"type":"rect","fillcolor":null,"line":{"color":null,"width":0,"linetype":[]},"yref":"paper","xref":"paper","x0":0.33987131589871317,"x1":0.66012868410128678,"y0":0,"y1":30.817766708177672,"yanchor":0.48028227480282276,"ysizemode":"pixel"}],"xaxis2":{"type":"linear","autorange":false,"range":[-0.017659663107329939,0.41369740396223537],"tickmode":"array","ticktext":["0.0","0.1","0.2","0.3","0.4"],"tickvals":[0,0.10000000000000002,0.19999999999999998,0.30000000000000004,0.40000000000000002],"categoryorder":"array","categoryarray":["0.0","0.1","0.2","0.3","0.4"],"nticks":null,"ticks":"","tickcolor":null,"ticklen":4.6492320464923198,"tickwidth":0,"showticklabels":true,"tickfont":{"color":"rgba(77,77,77,1)","family":"","size":14.877542548775427},"tickangle":-0,"showline":false,"linecolor":null,"linewidth":0,"showgrid":true,"domain":[0.33987131589871317,0.66012868410128678],"gridcolor":"rgba(235,235,235,1)","gridwidth":0.8453149175440583,"zeroline":false,"anchor":"y5","title":"","hoverformat":".2f"},"yaxis2":{"type":"linear","autorange":false,"range":[-0.21266900322188526,7.2014745617961538],"tickmode":"array","ticktext":["0","2","4","6"],"tickvals":[0,2,3.9999999999999996,6],"categoryorder":"array","categoryarray":["0","2","4","6"],"nticks":null,"ticks":"","tickcolor":null,"ticklen":4.6492320464923198,"tickwidth":0,"showticklabels":true,"tickfont":{"color":"rgba(77,77,77,1)","family":"","size":14.877542548775427},"tickangle":-0,"showline":false,"linecolor":null,"linewidth":0,"showgrid":true,"domain":[0.51971772519717729,1],"gridcolor":"rgba(235,235,235,1)","gridwidth":0.8453149175440583,"zeroline":false,"anchor":"x2","title":"","hoverformat":".2f"},"xaxis3":{"type":"linear","autorange":false,"range":[-0.017659663107329939,0.41369740396223537],"tickmode":"array","ticktext":["0.0","0.1","0.2","0.3","0.4"],"tickvals":[0,0.10000000000000002,0.19999999999999998,0.30000000000000004,0.40000000000000002],"categoryorder":"array","categoryarray":["0.0","0.1","0.2","0.3","0.4"],"nticks":null,"ticks":"","tickcolor":null,"ticklen":4.6492320464923198,"tickwidth":0,"showticklabels":true,"tickfont":{"color":"rgba(77,77,77,1)","family":"","size":14.877542548775427},"tickangle":-0,"showline":false,"linecolor":null,"linewidth":0,"showgrid":true,"domain":[0.67320464923204648,1],"gridcolor":"rgba(235,235,235,1)","gridwidth":0.8453149175440583,"zeroline":false,"anchor":"y3","title":"","hoverformat":".2f"},"yaxis3":{"type":"linear","autorange":false,"range":[-0.18044085582050376,4.6504516319879121],"tickmode":"array","ticktext":["0","1","2","3","4"],"tickvals":[0,1,2,3,4],"categoryorder":"array","categoryarray":["0","1","2","3","4"],"nticks":null,"ticks":"","tickcolor":null,"ticklen":4.6492320464923198,"tickwidth":0,"showticklabels":true,"tickfont":{"color":"rgba(77,77,77,1)","family":"","size":14.877542548775427},"tickangle":-0,"showline":false,"linecolor":null,"linewidth":0,"showgrid":true,"domain":[0.51971772519717729,1],"gridcolor":"rgba(235,235,235,1)","gridwidth":0.8453149175440583,"zeroline":false,"anchor":"x3","title":"","hoverformat":".2f"},"yaxis4":{"type":"linear","autorange":false,"range":[-0.21073440785958891,5.122754915364311],"tickmode":"array","ticktext":["0","1","2","3","4","5"],"tickvals":[0,1,1.9999999999999998,3,4,5],"categoryorder":"array","categoryarray":["0","1","2","3","4","5"],"nticks":null,"ticks":"","tickcolor":null,"ticklen":4.6492320464923198,"tickwidth":0,"showticklabels":true,"tickfont":{"color":"rgba(77,77,77,1)","family":"","size":14.877542548775427},"tickangle":-0,"showline":false,"linecolor":null,"linewidth":0,"showgrid":true,"domain":[0,0.48028227480282276],"gridcolor":"rgba(235,235,235,1)","gridwidth":0.8453149175440583,"zeroline":false,"anchor":"x","title":"","hoverformat":".2f"},"yaxis5":{"type":"linear","autorange":false,"range":[-0.27394671675930704,8.9386004696849994],"tickmode":"array","ticktext":["0","2","4","6","8"],"tickvals":[0,2,3.9999999999999996,6,8],"categoryorder":"array","categoryarray":["0","2","4","6","8"],"nticks":null,"ticks":"","tickcolor":null,"ticklen":4.6492320464923198,"tickwidth":0,"showticklabels":true,"tickfont":{"color":"rgba(77,77,77,1)","family":"","size":14.877542548775427},"tickangle":-0,"showline":false,"linecolor":null,"linewidth":0,"showgrid":true,"domain":[0,0.48028227480282276],"gridcolor":"rgba(235,235,235,1)","gridwidth":0.8453149175440583,"zeroline":false,"anchor":"x2","title":"","hoverformat":".2f"},"showlegend":true,"legend":{"bgcolor":null,"bordercolor":null,"borderwidth":0,"font":{"color":"rgba(0,0,0,1)","family":"","size":13.283520132835198},"title":{"text":"Partido","font":{"color":"rgba(0,0,0,1)","family":"","size":15.940224159402243}}},"hovermode":"closest","barmode":"relative"},"config":{"doubleClick":"reset","modeBarButtonsToAdd":["hoverclosest","hovercompare"],"showSendToCloud":false},"source":"A","attrs":{"bf0618a12":{"x":{},"y":{},"colour":{},"type":"scatter"},"bf079502e01":{"x":{},"y":{},"colour":{}}},"cur_data":"bf0618a12","visdat":{"bf0618a12":["function (y) ","x"],"bf079502e01":["function (y) ","x"]},"highlight":{"on":"plotly_click","persistent":false,"dynamic":false,"selectize":false,"opacityDim":0.20000000000000001,"selected":{"opacity":1},"debounce":0},"shinyEvents":["plotly_hover","plotly_click","plotly_selected","plotly_relayout","plotly_brushed","plotly_brushing","plotly_clickannotation","plotly_doubleclick","plotly_deselect","plotly_afterplot","plotly_sunburstclick"],"base_url":"https://plot.ly"},"evals":[],"jsHooks":[]}</script>
::: :::
## ¿Cómo calibrar el error de las encuestas (recordemos que las encuestas son de intención de voto a nivel nacional)?
::: {.cell}
```{.r .cell-code} # Agrupar varios partidos a la misma sigla surveys_clean_siglas <- surveys_clean |> mutate(Partido=case_when( str_detect(Partido, “PSOE”) ~ “PSOE”, str_detect(Partido, “PP”) ~ “PP”, str_detect(Partido, “C’s”) ~ “C’s”, str_detect(Partido, “PNV”) ~ “PNV”, str_detect(Partido, “BNGO”) ~ “BNG”, str_detect(Partido, “CIU”) ~ “CIU”, str_detect(Partido, “UP”) ~ “UP”, str_detect(Partido, “ERC”) ~ “ERC”, str_detect(Partido, “EH-BILDU”) ~ “EH-BILDU”, str_detect(Partido, “MP”) ~ “MP”, str_detect(Partido, “VOX”) ~ “VOX”, TRUE ~ “OTROS”, )) # Calcular el porcentaje de votos por partido en las elecciones votes_percentage <- election_tidy_with_siglas |> group_by(anno, siglas) |> summarise(total_votes = sum(votos, na.rm = TRUE), .groups = “drop_last”) |> mutate(percentage_votes = total_votes / sum(total_votes) * 100) |> ungroup()
# Calcular el porcentaje de intención de voto por partido en las encuestas survey_percentage <- surveys_clean_siglas |> mutate(date_elec = year(date_elec)) |> group_by(date_elec, Partido) |> summarise(mean_intention = mean(value, na.rm = TRUE), .groups = “drop_last”) |> mutate(percentage_intention = mean_intention / sum(mean_intention) * 100) |> ungroup()
# Unir ambas tablas y calcular el error error_calibration <- votes_percentage |> inner_join(survey_percentage, by = c(“anno” = “date_elec”, “siglas” = “Partido”)) |> mutate(error = abs(percentage_votes - percentage_intention)) ``` :::
## ¿Qué casas encuestadoras acertaron más y cuáles se desviaron más de los resultados?
::: {.cell}
```{.r .cell-code} # Calcular el porcentaje de error por casa encuestadora media_errors <- surveys_clean_siglas |> mutate(anno = year(date_elec)) |> group_by(anno, Partido, media) |> summarise( mean_intention = mean(value, na.rm = TRUE), .groups = “drop” ) |> mutate(percentage_intention = mean_intention / sum(mean_intention) * 100) |> inner_join(votes_percentage, by = c(“anno”, “Partido” = “siglas”)) |> mutate(error = abs(percentage_votes - percentage_intention))
# Calcular el error promedio por casa encuestadora accuracy_by_media <- media_errors |> group_by(media) |> summarise( mean_error = mean(error, na.rm = TRUE), .groups = “drop” ) |> arrange(mean_error)
# Identificar las casas encuestadoras más acertadas y más desviadas medios_mas_acertados <- accuracy_by_media |> slice_min(order_by = mean_error, n = 5) medios_menos_acertados <- accuracy_by_media |> slice_max(order_by = mean_error, n = 5) ``` :::
##Ejercicios extra Mendoza #Relación entre el censo y el voto mediante un mapa:
::: {.cell}
```{.r .cell-code} indice <- election_data|> filter(anno==2019) |> mutate(“key”=glue(“{codigo_provincia}-{codigo_municipio}”)) |> drop_na(votos_candidaturas, censo) |> mutate(“indice” = votos_candidaturas / censo)
mapa<-mapSpain::esp_get_munic() |> mutate(“key”=glue(“{cpro}-{cmun}”))
mapa_indice<- mapa |> left_join(indice, by=“key”)
#Grafico de la participacion
g_ind<-ggplot(mapa_indice)+ geom_sf(aes(alpha=indice, fill=indice), color=NA)+ scale_alpha_continuous(range = c(0.7,0.9))+ scale_fill_gradient2(low = “#b9feff”,mid=“#00c9ff”, high = “#040b64”, midpoint = mean(indice$indice), labels = scales::label_number(scale = 100, suffix=“%”))+ labs(fill=“PARTICIPACION”, title = “PARTICIPACION 2019”)+ theme_minimal()+ theme( axis.text = element_blank(), # Eliminar etiquetas de los ejes axis.ticks = element_blank(), # Eliminar marcas de los ejes legend.position = “bottom”, # Colocar la leyenda abajo legend.title = element_text(face = “bold”, size = 12), # Estilo de título de la leyenda legend.text = element_text(size = 8, angle = 30), # Estilo de texto de la leyenda strip.text = element_text(face = “bold”, size = 12), # Estilo de los títulos de las facetas plot.title = element_text(face = “bold”, size = 12, hjust = 0.5), # Estilo del título plot.subtitle = element_text(size = 12, hjust = 0.5) # Estilo del subtítulo )+ guides(alpha = “none”)
#Grafico del censo de España g_cen<-ggplot(mapa_indice)+ geom_sf(aes(alpha=censo, fill=censo), color=NA)+ scale_alpha_continuous(range = c(0.7,0.9))+ scale_fill_gradient2(low = “#b9feff”,mid=“#00c9ff”, high = “#040b64”, midpoint = mean(election_data$censo), transform = “log”,labels = scales::label_number(scale = 1, accuracy = 1))+ labs(title = “CENSO EN 2019”)+ theme_minimal()+ theme( axis.text = element_blank(), # Eliminar etiquetas de los ejes axis.ticks = element_blank(), # Eliminar marcas de los ejes legend.position = “bottom”, # Colocar la leyenda abajo legend.title = element_text(face = “bold”, size = 12), # Estilo de título de la leyenda legend.text = element_text(size = 8, angle = 30), # Estilo de texto de la leyenda strip.text = element_text(face = “bold”, size = 12), # Estilo de los títulos de las facetas plot.title = element_text(face = “bold”, size = 12, hjust = 0.5), # Estilo del título plot.subtitle = element_text(size = 12, hjust = 0.5) # Estilo del subtítulo )+ guides(alpha = “none”)
votos_censo<-g_ind+g_cen #Sumamos las graficas para compararlas print(votos_censo) ```
::: {.cell-output-display} :::
```{.r .cell-code} ganadores_mun<-election_tidy_with_siglas |> filter(anno==2019) |> mutate(“key”=glue(“{codigo_provincia}-{codigo_municipio}”)) |> group_by(key) |> drop_na(votos) |> slice_max(votos)
ganadores_mun<- ganadores_mun|> mutate(“Tipo_mun”= if_else(censo>median(ganadores_mun$censo),“Urbana”,“Rural”))
ggplot(ganadores_mun)+ geom_bar(aes(x = siglas, fill = siglas))+ scale_fill_manual(values = colores_partidos)+ facet_wrap(~Tipo_mun)+ labs(title = “GANADORES RURALES Y URBANOS 2019”, x= “PARTIDOS”, y= “VICTORIAS”)+ theme_minimal()+ theme( legend.position = “none”, # Quitar la leyenda strip.text = element_text(face = “bold”, size = 12), # Estilo de los títulos de las facetas plot.title = element_text(face = “bold”, size = 12, hjust = 0.5), # Estilo del título plot.subtitle = element_text(size = 12, hjust = 0.5), # Estilo del subtítulo axis.text = element_text(size = 10, angle = 30) )+ guides(alpha = “none”) ```
::: {.cell-output-display} ::: :::
## Ejercicios extras Marcos # ¿Qué 10 partidos obtuvieron mejores resultados en comparación con las predicciones de las encuestas?
::: {.cell}
{.r .cell-code} comparativa_partidos <- surveys_clean_siglas |> mutate(anno = year(date_elec)) |> group_by(anno, Partido) |> summarise( mean_intention = mean(value, na.rm = TRUE), .groups = "drop" ) |> mutate(percentage_intention = mean_intention / sum(mean_intention) * 100) |> inner_join(votes_percentage, by = c("anno", "Partido" = "siglas")) |> mutate(diferencia = percentage_votes - percentage_intention) |> arrange(desc(diferencia)) |> slice_max(order_by = diferencia, n = 10) :::
# ¿Cuál es la relación entre el tamaño del censo y la fragmentación del voto (índice de Herfindahl)? Representar los valores de fragmentación en un mapa por comunidades autonomas.
::: {.cell}
```{.r .cell-code} library(mapSpain) library(ggplot2) library(dplyr)
election_tidy_with_siglas <- election_tidy_with_siglas |> mutate(codigo_ccaa = substr(codigo_ccaa, 1, 2))
fragmentacion_ccaa <- election_tidy_with_siglas |> group_by(codigo_ccaa, Partido) |> summarise(votos_totales = sum(votos, na.rm = TRUE), censo_total = sum(censo, na.rm = TRUE), .groups = “drop”) |> group_by(codigo_ccaa) |> reframe( indice_herfindahl = sum((votos_totales / sum(votos_totales, na.rm = TRUE))^2, na.rm = TRUE), censo_total = unique(censo_total) )
mapa_ccaa <- mapSpain::esp_get_ccaa()
mapa_fragmentacion_ccaa <- mapa_ccaa |> left_join(fragmentacion_ccaa, by = c(“codauto” = “codigo_ccaa”))
ggplot(mapa_fragmentacion_ccaa) + geom_sf(aes(fill = indice_herfindahl), color = “gray90”, size = 0.1) + geom_sf_text( data = mapa_fragmentacion_ccaa |> filter(!is.na(indice_herfindahl)), aes(label = sprintf(“%.2f”, indice_herfindahl)), size = 2, color = “black”, fontface = “bold” ) + scale_fill_gradientn( colors = c(“lightcoral”, “gold”, “forestgreen”), name = “Fragmentación”, limits = c(0, 1), labels = scales::percent_format(accuracy = 1) ) + labs( title = “Fragmentación del Voto por Comunidades Autonomas en España”, subtitle = “Índice de Herfindahl”, fill = “Fragmentación (Herfindahl)” ) + coord_sf(datum = NA) + theme_minimal() + theme( plot.title = element_text(hjust = 0.5, size = 14, face = “bold”), plot.subtitle = element_text(hjust = 0.5, size = 12), legend.position = “right”, legend.title = element_text(size = 10), legend.text = element_text(size = 8), panel.grid = element_blank() ) ```
::: {.cell-output-display} ::: :::
::: {.cell}
```{.r .cell-code} # Rodri crecimiento_partido <- function(election_tidy) { # Calcular los votos totales por partido y año votos_agrupados <- election_tidy |> group_by(anno, Partido) |> summarise(total_votos = sum(votos, na.rm = TRUE), .groups = “drop”)
# Ordenar los datos por partido y año votos_ordenados <- votos_agrupados |> arrange(Partido, anno)
# Calcular el crecimiento o disminución porcentual entre elecciones consecutivas # Usamos lag() para obtener los votos del año anterior # En las elecciones en las que un determinado partido aparece por primera vez, el cambio # porcentual es 0 crecimiento <- votos_ordenados |> group_by(Partido) |> mutate(cambio_pct = ifelse(is.na(lag(total_votos)), 0, (total_votos - lag(total_votos)) / lag(total_votos) * 100)) |> ungroup()
return(crecimiento) } ``` :::
## Ejercicios extra Rodrigo # ¿Qué comunidad autónoma tiene la mayor participación electoral promedio en las elecciones entre 2008 y 2019?
::: {.cell}
```{.r .cell-code} codigo_a_comunidad <- c( “14” = “País Vasco”, “07” = “Castilla La Mancha”, “17” = “Comunidad Valenciana”, “01” = “Andalucía”, “08” = “Castilla y León”, “10” = “Extremadura”, “04” = “Islas Baleares”, “09” = “Cataluña”, “11” = “Galicia”, “02” = “Aragón”, “16” = “La Rioja”, “12” = “Madrid”, “15” = “Murcia”, “13” = “Navarra”, “03” = “Asturias”, “05” = “Canarias”, “06” = “Cantabria”, “18” = “Ceuta”, “19” = “Melilla” )
participacion_media_ccaa <- election_tidy_with_siglas |> group_by(codigo_ccaa) |> summarise( participacion_promedio = mean(votos_candidaturas, na.rm = TRUE) / mean(censo, na.rm = TRUE) * 100 )
participacion_media_ccaa <- participacion_media_ccaa |> mutate(nombre_ccaa = codigo_a_comunidad[as.character(codigo_ccaa)])
participacion <- ggplot(participacion_media_ccaa, aes(x = reorder(nombre_ccaa, -participacion_promedio), y = participacion_promedio, fill = nombre_ccaa)) + geom_bar(stat = “identity”, show.legend = FALSE, width = 0.8) + scale_fill_viridis_d(option = “D”, begin = 0.2, end = 0.8) + scale_y_continuous(limits = c(0, 100)) + labs( title = “Comunidad Autónoma con Mayor Participación Electoral Promedio (2008-2019)”, subtitle = “Participación promedio en las elecciones durante el período 2008-2019”, x = “Comunidad Autónoma”, y = “Participación Promedio (%)” ) + theme_minimal(base_size = 15) + theme( axis.text.x = element_text(angle = 45, hjust = 1, size = 13, family = “Arial”, color = “darkblue”), axis.text.y = element_text(size = 13, family = “Arial”, color = “darkblue”), axis.title.x = element_text(size = 14, face = “bold”, family = “Arial”, color = “darkred”), axis.title.y = element_text(size = 14, face = “bold”, family = “Arial”, color = “darkred”), plot.title = element_text(size = 11, face = “bold”, family = “Arial”, color = “darkgreen”), plot.subtitle = element_text(size = 12, family = “Arial”, color = “gray50”), plot.margin = margin(10, 20, 10, 20), panel.grid.major = element_line(color = “gray85”, size = 0.5), panel.grid.minor = element_blank(), panel.background = element_rect(fill = “white”, color = “white”))
participacion_int <- ggplotly(participacion) participacion_int ```
::: {.cell-output-display}
{=html} <div class="plotly html-widget html-fill-item" id="htmlwidget-bef7fd5c8e0001f4330a" style="width:960px;height:480px;"></div> <script type="application/json" data-for="htmlwidget-bef7fd5c8e0001f4330a">{"x":{"data":[{"orientation":"v","width":0.80000000000000071,"base":0,"x":[14],"y":[69.499428325606971],"text":"reorder(nombre_ccaa, -participacion_promedio): Andalucía<br />participacion_promedio: 69.49943<br />nombre_ccaa: Andalucía","type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(65,68,135,1)","line":{"width":1.8897637795275593,"color":"transparent"}},"name":"Andalucía","legendgroup":"Andalucía","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"orientation":"v","width":0.80000000000000071,"base":0,"x":[9],"y":[72.819197946309586],"text":"reorder(nombre_ccaa, -participacion_promedio): Aragón<br />participacion_promedio: 72.81920<br />nombre_ccaa: Aragón","type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(61,77,138,1)","line":{"width":1.8897637795275593,"color":"transparent"}},"name":"Aragón","legendgroup":"Aragón","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"orientation":"v","width":0.80000000000000071,"base":0,"x":[13],"y":[69.504630743393818],"text":"reorder(nombre_ccaa, -participacion_promedio): Asturias<br />participacion_promedio: 69.50463<br />nombre_ccaa: Asturias","type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(57,86,140,1)","line":{"width":1.8897637795275593,"color":"transparent"}},"name":"Asturias","legendgroup":"Asturias","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"orientation":"v","width":0.79999999999999893,"base":0,"x":[16],"y":[63.766890777281773],"text":"reorder(nombre_ccaa, -participacion_promedio): Canarias<br />participacion_promedio: 63.76689<br />nombre_ccaa: Canarias","type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(53,96,141,1)","line":{"width":1.8897637795275593,"color":"transparent"}},"name":"Canarias","legendgroup":"Canarias","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"orientation":"v","width":0.80000000000000071,"base":0,"x":[6],"y":[74.100650454269612],"text":"reorder(nombre_ccaa, -participacion_promedio): Cantabria<br />participacion_promedio: 74.10065<br />nombre_ccaa: Cantabria","type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(49,104,142,1)","line":{"width":1.8897637795275593,"color":"transparent"}},"name":"Cantabria","legendgroup":"Cantabria","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"orientation":"v","width":0.80000000000000027,"base":0,"x":[4],"y":[74.623571000356364],"text":"reorder(nombre_ccaa, -participacion_promedio): Castilla La Mancha<br />participacion_promedio: 74.62357<br />nombre_ccaa: Castilla La Mancha","type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(45,113,142,1)","line":{"width":1.8897637795275593,"color":"transparent"}},"name":"Castilla La Mancha","legendgroup":"Castilla La Mancha","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"orientation":"v","width":0.80000000000000071,"base":0,"x":[5],"y":[74.327854900772721],"text":"reorder(nombre_ccaa, -participacion_promedio): Castilla y León<br />participacion_promedio: 74.32785<br />nombre_ccaa: Castilla y León","type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(42,120,142,1)","line":{"width":1.8897637795275593,"color":"transparent"}},"name":"Castilla y León","legendgroup":"Castilla y León","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"orientation":"v","width":0.80000000000000071,"base":0,"x":[12],"y":[69.860360026233238],"text":"reorder(nombre_ccaa, -participacion_promedio): Cataluña<br />participacion_promedio: 69.86036<br />nombre_ccaa: Cataluña","type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(39,128,142,1)","line":{"width":1.8897637795275593,"color":"transparent"}},"name":"Cataluña","legendgroup":"Cataluña","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"orientation":"v","width":0.79999999999999716,"base":0,"x":[18],"y":[57.09917736957204],"text":"reorder(nombre_ccaa, -participacion_promedio): Ceuta<br />participacion_promedio: 57.09918<br />nombre_ccaa: Ceuta","type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(35,136,142,1)","line":{"width":1.8897637795275593,"color":"transparent"}},"name":"Ceuta","legendgroup":"Ceuta","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"orientation":"v","width":0.79999999999999982,"base":0,"x":[2],"y":[74.702589839420156],"text":"reorder(nombre_ccaa, -participacion_promedio): Comunidad Valenciana<br />participacion_promedio: 74.70259<br />nombre_ccaa: Comunidad Valenciana","type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(33,144,140,1)","line":{"width":1.8897637795275593,"color":"transparent"}},"name":"Comunidad Valenciana","legendgroup":"Comunidad Valenciana","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"orientation":"v","width":0.80000000000000071,"base":0,"x":[8],"y":[72.952855075425475],"text":"reorder(nombre_ccaa, -participacion_promedio): Extremadura<br />participacion_promedio: 72.95286<br />nombre_ccaa: Extremadura","type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(31,152,139,1)","line":{"width":1.8897637795275593,"color":"transparent"}},"name":"Extremadura","legendgroup":"Extremadura","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"orientation":"v","width":0.80000000000000071,"base":0,"x":[11],"y":[70.242139363869896],"text":"reorder(nombre_ccaa, -participacion_promedio): Galicia<br />participacion_promedio: 70.24214<br />nombre_ccaa: Galicia","type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(31,161,136,1)","line":{"width":1.8897637795275593,"color":"transparent"}},"name":"Galicia","legendgroup":"Galicia","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"orientation":"v","width":0.79999999999999716,"base":0,"x":[17],"y":[62.957362790613523],"text":"reorder(nombre_ccaa, -participacion_promedio): Islas Baleares<br />participacion_promedio: 62.95736<br />nombre_ccaa: Islas Baleares","type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(34,168,132,1)","line":{"width":1.8897637795275593,"color":"transparent"}},"name":"Islas Baleares","legendgroup":"Islas Baleares","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"orientation":"v","width":0.79999999999999982,"base":0,"x":[3],"y":[74.649785101222648],"text":"reorder(nombre_ccaa, -participacion_promedio): La Rioja<br />participacion_promedio: 74.64979<br />nombre_ccaa: La Rioja","type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(41,175,127,1)","line":{"width":1.8897637795275593,"color":"transparent"}},"name":"La Rioja","legendgroup":"La Rioja","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"orientation":"v","width":0.79999999999999993,"base":0,"x":[1],"y":[76.620125961761161],"text":"reorder(nombre_ccaa, -participacion_promedio): Madrid<br />participacion_promedio: 76.62013<br />nombre_ccaa: Madrid","type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(53,183,121,1)","line":{"width":1.8897637795275593,"color":"transparent"}},"name":"Madrid","legendgroup":"Madrid","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"orientation":"v","width":0.79999999999999716,"base":0,"x":[19],"y":[56.349595089528826],"text":"reorder(nombre_ccaa, -participacion_promedio): Melilla<br />participacion_promedio: 56.34960<br />nombre_ccaa: Melilla","type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(67,191,113,1)","line":{"width":1.8897637795275593,"color":"transparent"}},"name":"Melilla","legendgroup":"Melilla","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"orientation":"v","width":0.80000000000000071,"base":0,"x":[7],"y":[73.484620994329262],"text":"reorder(nombre_ccaa, -participacion_promedio): Murcia<br />participacion_promedio: 73.48462<br />nombre_ccaa: Murcia","type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(84,197,104,1)","line":{"width":1.8897637795275593,"color":"transparent"}},"name":"Murcia","legendgroup":"Murcia","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"orientation":"v","width":0.80000000000000071,"base":0,"x":[10],"y":[71.11843798571212],"text":"reorder(nombre_ccaa, -participacion_promedio): Navarra<br />participacion_promedio: 71.11844<br />nombre_ccaa: Navarra","type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(102,203,93,1)","line":{"width":1.8897637795275593,"color":"transparent"}},"name":"Navarra","legendgroup":"Navarra","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"orientation":"v","width":0.80000000000000071,"base":0,"x":[15],"y":[68.01957950403758],"text":"reorder(nombre_ccaa, -participacion_promedio): País Vasco<br />participacion_promedio: 68.01958<br />nombre_ccaa: País Vasco","type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(122,209,81,1)","line":{"width":1.8897637795275593,"color":"transparent"}},"name":"País Vasco","legendgroup":"País Vasco","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null}],"layout":{"margin":{"t":57.178912411789121,"r":26.567040265670396,"b":141.31336612045436,"l":76.048152760481514},"plot_bgcolor":"rgba(255,255,255,1)","font":{"color":"rgba(0,0,0,1)","family":"","size":19.9252801992528},"title":{"text":"<b> Comunidad Autónoma con Mayor Participación Electoral Promedio (2008-2019) <\/b>","font":{"color":"rgba(0,100,0,1)","family":"Arial","size":14.611872146118724},"x":0,"xref":"paper"},"xaxis":{"domain":[0,1],"automargin":true,"type":"linear","autorange":false,"range":[0.40000000000000002,19.600000000000001],"tickmode":"array","ticktext":["Madrid","Comunidad Valenciana","La Rioja","Castilla La Mancha","Castilla y León","Cantabria","Murcia","Extremadura","Aragón","Navarra","Galicia","Cataluña","Asturias","Andalucía","País Vasco","Canarias","Islas Baleares","Ceuta","Melilla"],"tickvals":[1,2,3,4,5,6.0000000000000009,7,8,9,10,11,12,13,14.000000000000002,15,16,17,18,19],"categoryorder":"array","categoryarray":["Madrid","Comunidad Valenciana","La Rioja","Castilla La Mancha","Castilla y León","Cantabria","Murcia","Extremadura","Aragón","Navarra","Galicia","Cataluña","Asturias","Andalucía","País Vasco","Canarias","Islas Baleares","Ceuta","Melilla"],"nticks":null,"ticks":"","tickcolor":null,"ticklen":4.9813200498132,"tickwidth":0,"showticklabels":true,"tickfont":{"color":"rgba(0,0,139,1)","family":"Arial","size":17.268576172685762},"tickangle":-45,"showline":false,"linecolor":null,"linewidth":0,"showgrid":true,"gridcolor":"rgba(217,217,217,1)","gridwidth":0.66417600664176002,"zeroline":false,"anchor":"y","title":{"text":"<b> Comunidad Autónoma <\/b>","font":{"color":"rgba(139,0,0,1)","family":"Arial","size":18.596928185969279}},"hoverformat":".2f"},"yaxis":{"domain":[0,1],"automargin":true,"type":"linear","autorange":false,"range":[-5,105],"tickmode":"array","ticktext":["0","25","50","75","100"],"tickvals":[0,24.999999999999996,50,75,100],"categoryorder":"array","categoryarray":["0","25","50","75","100"],"nticks":null,"ticks":"","tickcolor":null,"ticklen":4.9813200498132,"tickwidth":0,"showticklabels":true,"tickfont":{"color":"rgba(0,0,139,1)","family":"Arial","size":17.268576172685762},"tickangle":-0,"showline":false,"linecolor":null,"linewidth":0,"showgrid":true,"gridcolor":"rgba(217,217,217,1)","gridwidth":0.66417600664176002,"zeroline":false,"anchor":"x","title":{"text":"<b> Participación Promedio (%) <\/b>","font":{"color":"rgba(139,0,0,1)","family":"Arial","size":18.596928185969279}},"hoverformat":".2f"},"shapes":[{"type":"rect","fillcolor":null,"line":{"color":null,"width":0,"linetype":[]},"yref":"paper","xref":"paper","x0":0,"x1":1,"y0":0,"y1":1}],"showlegend":true,"legend":{"bgcolor":null,"bordercolor":null,"borderwidth":0,"font":{"color":"rgba(0,0,0,1)","family":"","size":15.940224159402243},"title":{"text":"","font":{"color":"rgba(0,0,0,1)","family":"","size":19.9252801992528}}},"hovermode":"closest","barmode":"relative"},"config":{"doubleClick":"reset","modeBarButtonsToAdd":["hoverclosest","hovercompare"],"showSendToCloud":false},"source":"A","attrs":{"bf059c2351e":{"x":{},"y":{},"fill":{},"type":"bar"}},"cur_data":"bf059c2351e","visdat":{"bf059c2351e":["function (y) ","x"]},"highlight":{"on":"plotly_click","persistent":false,"dynamic":false,"selectize":false,"opacityDim":0.20000000000000001,"selected":{"opacity":1},"debounce":0},"shinyEvents":["plotly_hover","plotly_click","plotly_selected","plotly_relayout","plotly_brushed","plotly_brushing","plotly_clickannotation","plotly_doubleclick","plotly_deselect","plotly_afterplot","plotly_sunburstclick"],"base_url":"https://plot.ly"},"evals":[],"jsHooks":[]}</script>
::: :::

#¿Cuál es la relación entre la participación electoral y el tamaño de la muestra de las encuestas?

encuestas_filtradas <- surveys_clean_siglas |> 
  filter(size >= 500)

participacion_por_partido <- encuestas_filtradas |> 
  group_by(Partido) |> 
  summarise(mean_participacion = mean(value, na.rm = TRUE))

partidos_colores <- c(
  "PSOE" = "#D50032",  
  "PP" = "#0056A0",    
  "C's" = "#FF6600",   
  "VOX" = "#006747",   
  "UP" = "#9B4F96",     
  "ERC" = "#D82B6D",    
  "PNV" = "#006A3E",    
  "MP" = "#03A9F4",     
  "EH-BILDU" = "#E32B5F",
  "CIU" = "#A7C3A4",    
  "BNG" = "#E60012",    
  "Otros" = "#BDBDBD"    
)

encuestas <- ggplot(participacion_por_partido, aes(x = reorder(Partido, -mean_participacion), y = mean_participacion, fill = Partido)) +
  geom_bar(stat = "identity", width = 0.7) +  
  scale_fill_manual(values = partidos_colores) +  
  labs(title = "Participación Electoral Promedio por Partido (Tamaño de Muestra > 500)",
       x = "Partido", y = "Participación Electoral Promedio (%)") +
  theme_minimal(base_size = 14) +  
  theme(
    axis.text.x = element_text(angle = 45, hjust = 1, size = 12),  
    axis.text.y = element_text(size = 12),  
    plot.title = element_text(size = 12, face = "bold", hjust = 0.5),  
    legend.position = "none",  
    panel.grid.major.x = element_blank(),  
    panel.grid.minor.x = element_blank()   
  ) +
  coord_flip()

encuestas_int <- ggplotly(encuestas)
encuestas_int